import { baseUrl } from './config.js'

function toplist(){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/toplist/detail`,
			method: 'GET',
			data: {},
			success: res => {
				let result = res.data.list
				result.length = 4
				resolve(result)
			},
			fail: () => {}
		});
	})
}

//歌单详情
function playlistInfo(id){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/playlist/detail?id=${id}`,
			method: 'GET',
			data:{},
			success:res => {
				let playlist = res.data.playlist
				let tracks = res.data.playlist.tracks
				let trackIds = res.data.playlist.trackIds
				let privileges = res.data.privileges
				let result = {
					playlist,
					tracks,
					trackIds,
					privileges
				}
				console.log(result)
				resolve(result)
			}
		})
	})
}

//歌曲详情
// /song/detail
function songDetail(id){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/song/detail?ids=${id}`,
			method: 'GET',
			data:{},
			success:res => {
				resolve(res)
			}
		})
	})
}

// 歌曲评论
// /comment/music (limit可选 评论个数)
function commontMusic(id){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/comment/music?id=${id}`,
			method: 'GET',
			data:{},
			success:res => {
				resolve(res)
			}
		})
	})
}

// 获得相似音乐
// /simi/song
function simiSong(id){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/simi/song?id=${id}`,
			method: 'GET',
			data:{},
			success:res => {
				resolve(res)
			}
		})
	})
}


// 获取歌词
// /lyric
function lyricMusic(id){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/lyric?id=${id}`,
			method: 'GET',
			data:{},
			success:res => {
				resolve(res)
			}
		})
	})
}

// 获取音乐url
// /song/url (br可选码率 最高无损--999000)
function songUrl(id){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/song/url?id=${id}`,
			method: 'GET',
			data:{},
			success:res => {
				resolve(res)
			}
		})
	})
}

//热搜榜详细
function hotSearch(){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/search/hot/detail`,
			method: 'GET',
			data:{},
			success:res => {
				resolve(res)
			}
		})
	})
}

//搜索建议
// /search/suggest?keywords= 海阔天空
function suggestSearch(keyValue){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/search/suggest?keywords=${keyValue}`,
			method: 'GET',
			data:{},
			success:res => {
				resolve(res)
			}
		})
	})
}

//搜索
// /search?keywords= 海阔天空
function keywordsSearch(keyValue){
	return new Promise((resolve,reject)=>{
		uni.request({
			url: `${baseUrl}/search?keywords=${keyValue}`,
			method: 'GET',
			data:{},
			success:res => {
				resolve(res)
			}
		})
	})
}

export { toplist, playlistInfo, songDetail, commontMusic, simiSong, lyricMusic, songUrl, hotSearch, suggestSearch, keywordsSearch}